package robotleMvc.frameworks.lightscene
{
	import _Pan3D.display3D.Display3DSprite;
	import _Pan3D.display3D.Display3DSprite;
	
	import _me.Scene_data;
	
	import flash.display3D.Context3D;
	import flash.display3D.Context3DBlendFactor;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.VertexBuffer3D;
	import flash.geom.Vector3D;
	import flash.display3D.Context3D;
	
	public class LightUvDisplay3DSprite extends Display3DSprite
	{
		private var timeNum:Number=1;
		private var context3D:Context3D;
		private var vertexBuffer:VertexBuffer3D;
		private var indexBuffer:IndexBuffer3D;
		public function LightUvDisplay3DSprite(context:Context3D)
		{
			super(context);
			context3D=context;
			makeMianpianModel();
		}
		override protected function setVa() : void {
			
			context3D.setTextureAt(1, _objData.texture);
			context3D.setVertexBufferAt(0, vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			context3D.setVertexBufferAt(1, vertexBuffer, 3, Context3DVertexBufferFormat.FLOAT_3);
			context3D.drawTriangles(indexBuffer, 0, -1);
		}
		private function makeMianpianModel() : void {
			
			var speed : Number = 300;
			var n : Number = 0;
			var m: Number=2;
			vertexBuffer  = context3D.createVertexBuffer(4, 6);
			vertexBuffer.uploadFromVector(Vector.<Number>(
				[-speed, speed, 0, 0, 0, 1,
					speed, speed, 0, 1/m, 0, 1, 
					speed, -speed, 0, 1/m, 1/m, 1, 
					-speed,-speed, 0, 0, 1/m, 1,]), 0, 4);
			var k : Array = [0, 2, 3, 0, 1, 2];
			
			indexBuffer  = context3D.createIndexBuffer(k.length);
			indexBuffer.uploadFromVector(Vector.<uint>(k), 0, k.length);
			
			
		}
		override public function updateMatrix():void{
			modelMatrix.identity();
			modelMatrix.prepend(Scene_data.cam3D.cameraMatrix);
			modelMatrix.prepend(posMatrix);
			updatePosMatrix();
		}
		override public function updatePosMatrix():void{
			posMatrix.identity();
			//posMatrix.prependRotation(-90, new Vector3D(1, 0, 0));
			posMatrix.prependTranslation(this._absoluteX, this._absoluteY, this._absoluteZ);
			posMatrix.prependScale(this._scale,this._scale,this._scale);
			posMatrix.prependRotation(-Scene_data.cam3D.angle_y , Vector3D.Y_AXIS);
			
		}
		
		override protected function resetVa() : void {
			_context.setVertexBufferAt(0, null);
			_context.setVertexBufferAt(1, null);
			context3D.setTextureAt(1, null);
		}
		
		override protected function setVc() : void {
			this.updateMatrix();
			//modelMatrix.identity();
			//modelMatrix.appendTranslation(0,0,-300)
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, modelMatrix, true);
			
			var m:Number=Math.sqrt(uvNum);
			timeNum=(timeNum+1)%uvNum;
			timeNum=1;
			var xx:Number=int(timeNum%m)/m;
			var yy:Number=int(timeNum/m)/m;
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, Vector.<Number>([xx, yy, 0, 1]));
		}
		public var uvNum:uint=4;
	}
}